package com.whmnx.chitchat.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.whmnx.chitchat.R;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.LoginBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.event.EventConstants;
import com.whmnx.chitchat.event.MessageEvent;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.main.LoginActivity;
import com.whmnx.chitchat.main.MainActivity;
import com.whmnx.chitchat.utils.ImageUtil;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;
import com.whmnx.chitchat.view.CommonDialog;

import org.apache.commons.lang3.StringEscapeUtils;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * author : huang
 * date   : 2020/7/22 0022 16:52
 * version: 1.0
 *
 * @author Administrator
 */
public class MyActivity extends BaseActivity {
    @BindView(R.id.iv_user)
    CircleImageView ivUser;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.my_activity);
        setHead_title(View.GONE);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void loadData() {
        ImageUtil.setImage(ivUser, SPUtil.getString(Constants.HEAD_IMG));
        tvName.setText(SPUtil.getString(Constants.NICKNAME));
        tvPhone.setText(SPUtil.getString(Constants.PHONE));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(EventConstants.FAMILY)) {
                ImageUtil.setImage(ivUser, SPUtil.getString(Constants.HEAD_IMG));
                tvName.setText(SPUtil.getString(Constants.NICKNAME));
            }
        }
    }



    @OnClick({R.id.ll_user, R.id.ib_back, R.id.tv_file, R.id.tv_fabu, R.id.tv_hei, R.id.tv_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_user:
                UItils.startActivity(MyInfoActivity.class);
            case R.id.ib_back:
                finish();
                break;
            case R.id.tv_file:
                UItils.startActivity(MyFileActivity.class);
                break;
            case R.id.tv_fabu:
                UItils.startActivity(MyFabuActivity.class);
                break;
            case R.id.tv_hei:
                UItils.startActivity(MyHeiActivity.class);
                break;
            case R.id.tv_out:
                String s2 = "确定退出吗?";
                new CommonDialog(this, R.style.dialog, s2, (dialog, confirm) -> {
                    if (confirm) {
                        SPUtil.put(Constants.WELCOME,false);
                        SPUtil.put(Constants.LOGIN, false);
                        MainActivity.reliableClient.LogOut();
                        MainActivity.reliableClient = null;
                        Map<String, Object> params = new HashMap<>();
                        params.put("UserID",SPUtil.getString(Constants.USERID));
                        params.put("DeviceToken","");
                        params.put("MoveType","1");
                        HttpClient.get(this, Constants.UPDATEUSERDEVICETOKEN, params,new CallBack<LoginBean>() {
                            @Override
                            public void onSuccess(LoginBean result) {

                            }
                        });
                        Intent intent2 = new Intent(this, LoginActivity.class);
                        intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent2);
                    }
                    dialog.dismiss();
                }).show();
                break;
            default:
        }
    }
}
